﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class BooksManager
    {
        public BookInfo GetBookById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryBook = from d in db.Books
                                    where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                select new BookInfo { Id = d.Id, Code = d.Code, Name = d.Name, Status = d.Status, DiscountWholesale = (float)d.DiscountWholesale, Price = (float)d.Price, DiscountRetail = (float)d.DiscountRetail, Publisher = d.Publisher, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryBook.OrderBy(d => d.Code).First();
            }

        }
        public BookInfo GetBookByCode(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryBook = from d in db.Books
                                where d.Status.Equals(1)
                                where d.Code.Equals(code)
                                select new BookInfo { Id = d.Id, Code = d.Code, Name = d.Name, Status = d.Status, DiscountWholesale = (float)d.DiscountWholesale, Price = (float)d.Price, DiscountRetail = (float)d.DiscountRetail, Publisher = d.Publisher, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryBook.OrderBy(d => d.Code).First();
            }

        }

        public IList<BookInfo> ListBook()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryBook = from d in db.Books
                                where d.Status.Equals(1)
                                select new BookInfo { Id = d.Id, Code = d.Code, Name = d.Name, Status = d.Status, DiscountWholesale = (float)d.DiscountWholesale, Price = (float)d.Price, DiscountRetail = (float)d.DiscountRetail,Publisher=d.Publisher, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryBook.OrderBy(d => d.Code).ToList();
            }
        }

        public void CreateBook(Book book)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Books.InsertOnSubmit(book);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="book"></param>
        public void UpdateBook(Book book)
        {
            using (var db = new QLKDSDataContext())
            {
                var _book = db.Books.Single(d => d.Id.Equals(book.Id));

                _book.Code = book.Code;
                _book.Name = book.Name;
                _book.Price = book.Price;
                _book.DiscountRetail = book.DiscountRetail;
                _book.DiscountWholesale = book.DiscountWholesale;
                book.Publisher = book.Publisher;
                _book.Status = book.Status;

                _book.Description = book.Description;
                _book.CreatedBy = book.CreatedBy;
                _book.CreatedDate = book.CreatedDate;
                _book.ModifiedBy = book.ModifiedBy;
                _book.ModifiedDate = book.ModifiedDate;
 
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteBook(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _book = db.Books.Single(d => d.Id.Equals(id));

                _book.Status = 0;

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void RemoveBook(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _book = db.Books.Single(d => d.Id.Equals(id));

                db.Books.DeleteOnSubmit(_book);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool HasCodeExisted(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Books
                            where d.Code.Equals(code)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasCodeDuplicated(string code, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Books
                            where d.Code.Equals(code)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

    }
}
